<?php
/**
 * 用户信息控制器
 * @author ZhouYP
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class UserController extends WahahabaseController{

	protected $users_model,$role_model,$department_model;
	public function _initialize() {
		parent::_initialize();
		$this->users_model = D("Common/Users");
		$this->role_model = D("Common/Role");
		$this->department_model=D("Common/Department");
	}

	public function index(){
		/**搜索条件**/
		$where = array("u.is_deleted"=>array('neq', 1));
		$user_login = trim(I('request.user_login'));
		$real_name = trim(I('request.real_name'));
		if($user_login){
			$where['u.user_login'] = array('like', "%$user_login%");
		}	
		if($real_name){
			$where['u.real_name'] = array('like', "%$real_name%");
		}
		
		$count=$this->users_model->alias("u")->where($where)->count();
		$page = $this->page($count, 20);
        $users = $this->users_model->alias("u")
            ->field('u.id,u.user_login,department_name,u.role_id,s.real_name add_user,u.real_name,u.last_login_ip,u.last_time')
            ->join(array(
            	 ' LEFT JOIN '.C('DB_PREFIX').'department d ON d.id=u.department_id',
            	 ' LEFT JOIN '.C('DB_PREFIX').'users s ON s.add_user = s.id',
            	))
            ->where($where)
            ->order("u.department_id,u.user_login")
            ->limit($page->firstRow, $page->listRows)
            ->select();//exit($this->users_model->getLastSql());
        // $users = M('department  as  a')->join('hzzy_users  as  b ')->where("b.department_id=a.id")->limit($page->firstRow, $page->listRows)->select();
        // var_dump($users);exit();
		$roles_src=$this->role_model->select();
		$roles=array();
		foreach ($roles_src as $r){
			$roleid=$r['id'];
			$roles["$roleid"]=$r;
		}
		$this->assign("page", $page->show('Admin'));
		$this->assign("roles",$roles);
		$this->assign("users",$users);
		$this->display();
	}

	public function add(){
        $Department=$this->department_model->where(array('is_deleted'=>0))->order("id DESC")->select();
        $this->assign("Department",$Department);
		$roles=$this->role_model->where(array('status' => 1))->order("id DESC")->select();
		$this->assign("roles",$roles);
		$this->display();
	}

	public function add_post(){
		if(IS_POST){
			if(!empty($_POST['role_id'])){
				if ($this->users_model->create()!==false) {
					$data = array_merge($_POST, $this->saveData());
					$result=$this->users_model->add($data);
					if ($result!==false) {
						$role_user_model=M("RoleUser");
						foreach ($role_ids as $role_id){
							if(sp_get_current_admin_id() != 1 && $role_id == 1){
								$this->error("为了系统的安全，非系统创建者不可创建超级管理员！");
							}
							$role_user_model->add(array("role_id"=>$role_id,"user_id"=>$result));
						}
						$this->success("添加成功！", U("user/index"));
					} else {
						$this->error("添加失败！");
					}
				} else {
					$this->error($this->users_model->getError());
				}
			}else{
				$this->error("请为此用户选择用户权限！");
			}

		}
	}

	public function edit(){
	    $id = I('get.id',0,'intval');
		$roles=$this->role_model->where(array('status' => 1))->order("id DESC")->select();
		$this->assign("roles",$roles);
		//$role_user_model=M("RoleUser");
		//$role_ids=$role_user_model->where(array("id"=>$id))->getField("role_id",true);
		//$this->assign("role_id",$role_ids);
        $user = M('department  as  a')->join('hzzy_users  as  b ')->where(array("b.id"=>$id))->find();
		$this->assign($user);
		$depar=$this->department_model->field("id,department_name")->where("is_deleted <> 1")->order("id DESC")->select();
		// var_dump($depar);exit();
        $this->assign("depar",$depar);
        //$this->assign($dpartment);
		$this->display();
	}

	public function edit_post(){
		if (IS_POST) {
			if(!empty($_POST['role_id'])){
				if(empty($_POST['user_pass'])){
					unset($_POST['user_pass']);
				}
				if ($this->users_model->create()!==false) {

					$data = array_merge($_POST, $this->saveData(2));
					$result=$this->users_model->save($data);
					if ($result!==false) {
						$uid = I('post.id',0,'intval');
						$role_user_model=M("RoleUser");
						$role_user_model->where(array("user_id"=>$uid))->delete();
						foreach ($role_ids as $role_id){
							if(sp_get_current_admin_id() != 1 && $role_id == 1){
								$this->error("为了系统的安全，非创建者不可创建超级管理员！");
							}
							$role_user_model->add(array("role_id"=>$role_id,"user_id"=>$uid));
						}
						$this->success("保存成功！",U("user/index"));
					} else {
						$this->error("保存失败！");
					}
				} else {
					$this->error($this->users_model->getError());
				}
			}else{
				$this->error("请为此用户指定角色！");
			}

		}
	}

	/**
	 *  删除
	 */
	public function delete(){
	    $id = I('get.id',0,'intval');
		if($id==1){
			$this->error("最高管理员不能删除！");
		}

		$data = array(
			'id'	=> $id,
			'is_deleted' => 1
		);
		if ($this->users_model->save($data) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

	public function userinfo(){
		$id=sp_get_current_admin_id();
		$user=$this->users_model->where(array("id"=>$id))->find();
		$this->assign($user);
		$this->display();
	}

	public function userinfo_post(){
		if (IS_POST) {
			$_POST['id']=sp_get_current_admin_id();
			$create_result=$this->users_model
			->field("id,user_nicename,sex,birthday,user_url,signature")
			->create();
			if ($create_result!==false) {
				if ($this->users_model->save()!==false) {
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->users_model->getError());
			}
		}
	}

    public function ban(){
        $id = I('get.id',0,'intval');
    	if (!empty($id)) {
    		$result = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','0');
    		if ($result!==false) {
    			$this->success("管理员停用成功！", U("user/index"));
    		} else {
    			$this->error('管理员停用失败！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }

    public function cancelban(){
    	$id = I('get.id',0,'intval');
    	if (!empty($id)) {
    		$result = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','1');
    		if ($result!==false) {
    			$this->success("管理员启用成功！", U("user/index"));
    		} else {
    			$this->error('管理员启用失败！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }



}